import time
import email
import smtplib
import ssl
from email import encoders
from email.mime.multipart import MIMEMultipart, MIMEBase
from email.mime.text import MIMEText
from config.config import reciver, passwd, sender
subject = time.strftime('%y-%m-%d %H:%M:%S')+"测试报告汇总"
body = "具体相关详情请参考文件附件"

message = MIMEMultipart()
message["From"] = sender
message["To"] = reciver
message["Subject"] = subject

# 邮件主体
message.attach(MIMEText(body, 'plain'))


zipfile = 'reports.zip'
with open(zipfile, 'rb') as f:
    # 文件类型
    part = MIMEBase("application", "octet-stream")
    part.set_payload(f.read())

encoders.encode_base64(part)

part.add_header(
    "Content-Disposition",
    f"attachment; filename= {zipfile}",
)
message.attach(part)

context = ssl.create_default_context()

with smtplib.SMTP_SSL('smtp.163.com', 465, context=context) as server:
    server.login(user=sender, password=passwd)
    server.send_message(message)
